package com.atguigu.gmall.common.config.exception.handler;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//controller异常切面
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 和业务有关的异常，要用GmallException
     * @param e
     * @return
     */
    @ExceptionHandler(GmallException.class)
    public Result handlerGmallException(GmallException e){

        Result<Object> fail = Result.fail();
        fail.setCode(e.getCode());
        fail.setMessage(e.getMessage());
        return fail;
    }
    /**
     * 处理所有controller出现异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e){

        Result<Object> fail = Result.fail();
        fail.setMessage(e.getMessage());
        return fail;
    }
}
